package com.bz.xtcx.manager.controller;

import com.bz.xtcx.manager.comom.XtcxException;
import com.bz.xtcx.manager.entity.CmsMeeting;
import com.bz.xtcx.manager.persist.conditions.MeetingCondition;
import com.bz.xtcx.manager.service.ICmsMeetingService;
import com.bz.xtcx.manager.utils.WzStrUtil;
import com.bz.xtcx.manager.vo.VoResponse;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
@RequestMapping("meeting")
public class MeetingController {
    /** 日志. */
    private final Logger logger = LoggerFactory.getLogger(MeetingController.class);

    @Autowired
    private ICmsMeetingService cmsMeetingService;

    @PostMapping("page")
    public VoResponse getAllMeetings(
            @RequestBody(required=false) MeetingCondition query//,
//            @RequestParam("pageNum") int pageNum,
//            @RequestParam("pageSize") int pageSize
            ) {
        VoResponse voRes = new VoResponse();
        int usedPageNum = 1;
        int usedPageSize = 5;
        if (null != query) {
            if (null != query.getCurrPage() && 0 < query.getCurrPage()) {
                usedPageNum = query.getCurrPage();
            }
            if (null != query.getPageSize() && 0 < query.getPageSize()) {
                usedPageSize = query.getPageSize();
            }
            if (null != query.getIsFormOrToMe() && 1 != query.getIsFormOrToMe() && 2 != query.getIsFormOrToMe()) {
                voRes.setFail(voRes);
                voRes.setMessage("只能选我发起的或向我发起的");
                return voRes;
            }
            if (null != query.getTypeId()
                    && 0 != query.getTypeId()
                    && 1 != query.getTypeId()
                    && 2 != query.getTypeId()
                    && 3 != query.getTypeId()) {
                voRes.setFail(voRes);
                voRes.setMessage("请选择正确的对接类别");
                return voRes;
            }
            if (null != query.getCheckStatus()
                    && -1 != query.getCheckStatus()
                    && 0 != query.getCheckStatus()
                    && 1 != query.getCheckStatus()
                    && 2 != query.getCheckStatus()
                    && 3 != query.getCheckStatus()) {
                voRes.setFail(voRes);
                voRes.setMessage("请选择正确的状态");
                return voRes;
            }
        }
        try {
            voRes.setData(cmsMeetingService.getPageByCondition(query, usedPageNum, usedPageSize));
        } catch (XtcxException e) {
            logger.error("查询错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("查询错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            logger.error("系统错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
        return voRes;
    }

    @PostMapping("userpage")
    public VoResponse getMeetingsForUser(
            @RequestBody(required=false) MeetingCondition query//,
//            @RequestParam("pageNum") int pageNum,
//            @RequestParam("pageSize") int pageSize
            ) {
        VoResponse voRes = new VoResponse();
        int usedPageNum = 1;
        int usedPageSize = 5;
        if (null != query) {
            if (null != query.getCurrPage() && 0 < query.getCurrPage()) {
                usedPageNum = query.getCurrPage();
            }
            if (null != query.getPageSize() && 0 < query.getPageSize()) {
                usedPageSize = query.getPageSize();
            }
            if (null != query.getIsFormOrToMe() && 1 != query.getIsFormOrToMe() && 2 != query.getIsFormOrToMe()) {
                voRes.setFail(voRes);
                voRes.setMessage("只能选我发起的或向我发起的");
                return voRes;
            }
            if (null != query.getTypeId()
                    && 0 != query.getTypeId()
                    && 1 != query.getTypeId()
                    && 2 != query.getTypeId()
                    && 3 != query.getTypeId()) {
                voRes.setFail(voRes);
                voRes.setMessage("请选择正确的对接类别");
                return voRes;
            }
            if (null != query.getCheckStatus()
                    && -1 != query.getCheckStatus()
                    && 0 != query.getCheckStatus()
                    && 1 != query.getCheckStatus()
                    && 2 != query.getCheckStatus()
                    && 3 != query.getCheckStatus()) {
                voRes.setFail(voRes);
                voRes.setMessage("请选择正确的状态");
                return voRes;
            }
//            if (WzStrUtil.isBlank(query.getTargeterCN())) {
//                voRes.setFail(voRes);
//                voRes.setMessage("缺少被对接方信息");
//                return voRes;
//            }
        }
        try {
            voRes.setData(cmsMeetingService.getPageForUser(query, usedPageNum, usedPageSize));
        } catch (XtcxException e) {
            logger.error("查询错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("查询错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            logger.error("系统错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
        return voRes;
    }

    @GetMapping
    public VoResponse get(@RequestParam("id") String id) {
        VoResponse voRes = new VoResponse();
        if (WzStrUtil.isBlank(id)) {
            voRes.setFail(voRes);
            voRes.setMessage("缺少参数");
            return voRes;
        }
        try {
            voRes.setData(cmsMeetingService.getById(id));
        } catch (XtcxException e) {
            logger.error("查询错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("查询错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            logger.error("系统错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
        return voRes;
    }

    @PutMapping("refuse")
    public VoResponse refuseMeeting(@RequestBody Map<String,String> map){
        if (null == map || WzStrUtil.isBlank(map.get("id"))) {
            VoResponse voRes = new VoResponse();
            voRes.setFail(voRes);
            voRes.setMessage("缺少参数");
            return voRes;
        }
        String whyStr = map.get("checkContent");
        if (WzStrUtil.isBlank(whyStr)) {
            whyStr = "未填写拒绝原因";
        }
        VoResponse voRes = null;
        try {
            voRes = cmsMeetingService.refuse(map.get("id"), whyStr);
        } catch (XtcxException e) {
            logger.error("查询错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("查询错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            logger.error("系统错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
        return voRes;
    }

    @PutMapping("agree")
    public Object agreeMeeting(@RequestBody Map<String,String> map){
        if (null == map || WzStrUtil.isBlank(map.get("id"))) {
            VoResponse voRes = new VoResponse();
            voRes.setFail(voRes);
            voRes.setMessage("缺少参数");
            return voRes;
        }
        VoResponse voRes = null;
        try {
            voRes = cmsMeetingService.agree(map.get("id"));
        } catch (XtcxException e) {
            logger.error("查询错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("查询错误：" + e.getMessage());
            return voRes;
        } catch (Exception e) {
            logger.error("系统错误：" + e.getMessage(), e);
            voRes.setFail(voRes);
            voRes.setMessage("系统错误：" + e.getMessage());
            return voRes;
        }
        return voRes;
    }

    @ApiOperation(value = "重新发起对接", httpMethod = "PUT", notes = "用户发起对接请求")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeId", value = "对接类别", dataType = "Integer", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fcn", value = "发起联系人姓名", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fcp", value = "发起联系人电话", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "fcm", value = "发起联系人邮箱", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "fca", value = "发起联系人地址", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "appId", value = "内容ID", dataType = "String", required = true, paramType = "query"),
            @ApiImplicitParam(name = "appNote", value = "申请说明", dataType = "String", required = true, paramType = "query")
    })
//    @PutMapping(value = {"reapply"})
//    public VoResponse meetingReapply(@RequestParam(value = "typeId", required = true) Integer typeId,
//                                   @RequestParam(value = "fcn", required = true) String fcn,
//                                   @RequestParam(value = "fcp", required = true) String fcp,
//                                   @RequestParam(value = "fcm", required = true) String fcm,
//                                   @RequestParam(value = "fca", required = false) String fca,
//                                   @RequestParam(value = "appId", required = true) String appId,
//                                   @RequestParam(value = "appNote", required = true) String appNote) {
    @PutMapping(value = {"reapply"})
    public VoResponse meetingReapply(@RequestBody Map<String,Object> map) {
        Integer typeId = (Integer) map.get("typeId");
        String fcn = (String) map.get("fcn");
        String fcp = (String) map.get("fcp");
        String fcm = (String) map.get("fcm");
        String fca = (String) map.get("fca");
        String appId = (String) map.get("appId");
        String appNote = (String) map.get("appNote");
        VoResponse resp = new VoResponse();
        if (null == typeId && 0 != typeId && 1 != typeId && 2 != typeId && 3 != typeId) {
            resp.setFail(resp);
            resp.setMessage("只能进行专家、需求、成果、服务对接");
            return resp;
        }
        if (WzStrUtil.isBlank(fcn) || WzStrUtil.isBlank(fcp) || WzStrUtil.isBlank(fcm)) {
            resp.setFail(resp);
            resp.setMessage("发起联系人姓名、手机号、电子邮箱必须填写");
            return resp;
        }
        if (WzStrUtil.isBlank(appId)) {
            resp.setFail(resp);
            resp.setMessage("丢失对接内容，请重试");
            return resp;
        }
        if (WzStrUtil.isBlank(appNote)) {
            resp.setFail(resp);
            resp.setMessage("请填写申请说明，以便让对方了解您");
            return resp;
        }
        CmsMeeting cm = new CmsMeeting();
        cm.setTypeId(typeId);
        cm.setFromerContactsName(fcn);
        cm.setFromerContactsPhone(fcp);
        cm.setFromerContactsMail(fcm);
        cm.setFromerContactsAddr(fca);
        cm.setApplicationContentId(appId);
        cm.setApplicationNote(appNote);
        try {
            cmsMeetingService.add(cm);
        } catch (XtcxException e) {
            logger.error("对接错误：" + e.getMessage(), e);
            resp.setFail(resp);
            resp.setMessage("对接错误：" + e.getMessage());
            return resp;
        } catch (Exception e) {
            logger.error("系统错误：" + e.getMessage(), e);
            resp.setFail(resp);
            resp.setMessage("系统错误：" + e.getMessage());
            return resp;
        }
        return resp;
    }
}
